'
'   XC Engine (project Minotaur)
'   Copyright (C) 2009 Stanislav Hordiyenko
'
'   This program is free software; you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the Free Software Foundation; either version 3 of the License, or
'   (at your option) any later version.
'
'   This program is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'   GNU General Public License for more details.
'
'   You should have received a copy of the GNU General Public License
'   along with this program; if not, see http://www.gnu.org/licenses
'   or write to the Free Software Foundation,Inc., 51 Franklin Street,
'   Fifth Floor, Boston, MA 02110-1301 USA
'

Class TestSuite
	Private objTestScriptList
	Private objTestSuiteInformation
	Private blnLoaded
	
	' == Initialize and Terminate =========================================
	Private Sub Class_Initialize()
		Set objTestScriptList = New ArrayList
		Set objTestSuiteInformation = CreateObject("Scripting.Dictionary")
		
		blnLoaded = False
	End Sub
	
	Private Sub Class_Terminate()
		Set objTestScriptList = Nothing
		Set objTestSuiteInformation = Nothing
	End Sub
	
	' == Properties =========================================
	Public Property Get TestScriptCount
		TestScriptCount = objTestScriptList.Count
	End Property
	
	Public Property Get AvaragePerformedTime
		AvaragePerformedTime = 0
		
		For i=1 To objTestScriptList.Count		
			If Not objTestScriptList(i).SkipExecution Then
				' Check that avarageperformedtime is float, in other case, rise an error
				AvaragePerformedTime = AvaragePerformedTime + CDbl(objTestScriptList(i).GetTestScriptInfo("AverageExecutionTime"))
			End If
		Next
		
		AvaragePerformedTime = Round(AvaragePerformedTime, 2)
	End Property
	
	' == Methods =========================================
	Public Sub SetProperty(ByVal strPropertyName, ByVal varPropertyValue)
		If Not objTestSuiteInformation.Exists(strPropertyName) Then
			objTestSuiteInformation.Add strPropertyName, varPropertyValue
		Else
			objTestSuiteInformation(strPropertyName) = varPropertyValue
		End If
	End Sub
	
	Public Function GetProperty(ByVal strPropertyName)
		If Not objTestSuiteInformation.Exists(strPropertyName) Then
			Err.Raise 5000, "TestSuite->GetProperty", "Error Description"
		End If
		
		GetProperty = objTestSuiteInformation(strPropertyName)
	End Function
	
	Public Function Load(ByVal strFileName)
		Set objSys = New Sys
	
		strFileName = strFileName & ".test-suite.xml"
	
		strPathToTestSuite = objSys.GetPathToFileByFileName(GLOBAL_APPLICATION_PATH & "test artefacts\", strFileName)
		
		If strPathToTestSuite = "" Then
			Err.Raise 5000, "TestSuite->Load", "Error Description"
		End If
	
		Set objXmlDocument = CreateObject("MSXML2.DOMDocument.6.0")
		
		objXmlDocument.Async = False

		If Not objXmlDocument.Load(strPathToTestSuite) Then
			Err.Raise 5000, "TestSuite->Load", "Error occured during loading Xml file"
		End If

		Set objXmlTestSuiteInformation = objXmlDocument.GetElementsByTagName("General")
		
		If objXmlTestSuiteInformation.Length = 0 Then
			Err.Raise 5000, "TestSuite->Load", "Error Description"
			
			Exit Function
		End If

		Set colXmlGeneralInformation = objXmlTestSuiteInformation(0).ChildNodes
		
		For i=0 To colXmlGeneralInformation.Length-1
			Call SetProperty(colXmlGeneralInformation(i).TagName, Trim(colXmlGeneralInformation(i).FirstChild.NodeValue))
		Next

		Set objXmlTestScriptList = objXmlDocument.GetElementsByTagName("TestScript")

		For i=0 To objXmlTestScriptList.Length-1
			strTestScriptFileName = objXmlTestScriptList(i).GetAttribute("Name")
			strExecute = objXmlTestScriptList(i).GetAttribute("Execute")

			Set objTestScript = New TestScript
			
			' Here should be added block to catch the errors. What to do, if the test scipt doesn't found? Performed other test scripts, or notify user about this?
			objTestScript.Load strTestScriptFileName

			If UCase(strExecute) = "FALSE" Then
				objTestScript.SkipExecution = True
			Else
				objTestScript.SkipExecution = False
			End If
			
			objTestScriptList.Add objTestScript
		Next
		
		blnLoaded = True
	End Function
	
	Public Default Function Item(ByVal intIndex)
		If intIndex < 1 And intIndex > objTestScriptList.Count Then
			Err.Raise 5000, "TestSuite->Item", "Error Description"
			
			Exit Function
		End If
		
		Set Item = objTestScriptList(intIndex)
	End Function
End Class